package com.thirteen.app.service.impl;

import com.thirteen.app.daoTest.QuestionRepositoryDao;
import com.thirteen.app.dto.QuestionAnswerDto;
import com.thirteen.app.entity.Answer;
import com.thirteen.app.entity.Question;
import com.thirteen.app.entity.QuestionRepository;
import com.thirteen.app.service.QuestionRepositoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("questionRepositoryService")
public class QuestionRepositoryServiceImpl implements QuestionRepositoryService {


    @Resource
    QuestionRepositoryDao questionRepositoryDao;

//    zql&ar

    //该方法用于新增题库
    @Override
    public boolean insert(QuestionRepository questionrepository) {
        return this.questionRepositoryDao.insert(questionrepository) > 0;

    }

    //该方法用于创建题目
    @Override
    public int createQu(String content, String courseName, String analysis, Integer repositoryId, Integer questionType, Integer answerCount, String solution, List<Answer> list) {
        System.out.println("题目:" + content + "分析:" + analysis);
        //向question表插入数据
        Question question = new Question();
        question.setContent(content);//设置题目的标题
        if (answerCount != null) {       //如果是填空或者多选设置答案数量
            question.setAnswerCount(answerCount);
        }

        question.setQuestionType(questionType);//设置题目类型id
        question.setAnalysis(analysis);        //设置题目解析

        questionRepositoryDao.insertQu(question);

        //向RepositoryQuestion表插入数据
        questionRepositoryDao.insertRepositoryQuestion(repositoryId, questionType, question.getQuestionId());

        for (Answer answer : list) {
            answer.setQuestionId(question.getQuestionId());   //设置答案对应的题目id
        }

        for (Answer answer : list) {
            answer.setIsRight(false);    //初始化答案的IsRight字段
        }
        //向答案表插入数据
        if (questionType == 1) {  //处理单选题
            list.get(Integer.parseInt(solution)).setIsRight(true);  //根据前端传过来的solution设置正确选项的isRight=1
        } else if (questionType == 2) {  //处理多选题
            String[] solutions = solution.split(",");  //用逗号把正确答案下标列表分割

            for (String s : solutions) {
                list.get(Integer.parseInt(s)).setIsRight(true);
            }
        } else if (questionType == 3) {   //处理判断
            list.get(Integer.parseInt(solution)).setIsRight(true);//设置正确选项的isRight为1
            list.get(Integer.parseInt(solution)).setContent("对");//设置答案的内容
            if (Integer.parseInt(solution) == 0) {
                list.get(0).setIsRight(true);
                list.get(0).setContent("对");
                list.get(1).setIsRight(false);
                list.get(1).setContent("错");
            }

        } else {   //处理填空和简答
            for (Answer answer : list) {
                answer.setIsRight(true);
                answer.setContent(solution);
            }
        }
        return questionRepositoryDao.insertAnswer(list);
    }

    //該方法用于处理答案
    protected void handle(QuestionAnswerDto questionAnswerDto) {
        if (questionAnswerDto != null) {
            if (questionAnswerDto.getQuestionType() == 1) {   //处理单选题
                String rightAnswer = "";
                for (Answer i : questionAnswerDto.getChoicesList()) {
                    if (i.getIsRight()) {              //找到isRight为1的那个选项。得出正确答案
                        rightAnswer = i.getTag();
                        break;
                    }
                }
                questionAnswerDto.setSolution(rightAnswer);
            } else if (questionAnswerDto.getQuestionType() == 2) {    //处理多选题

                String rightAnswer = "";
                for (Answer answer : questionAnswerDto.getChoicesList()) {
                    if (answer.getIsRight()) {
                        rightAnswer += answer.getTag() + ",";    //将正确答案用,连接起来
                    }
                }

                questionAnswerDto.setSolution(rightAnswer.substring(0, rightAnswer.length() - 1));
                System.out.println(questionAnswerDto.getSolution());
            } else if (questionAnswerDto.getQuestionType() == 3) {    //处理判断题
                if (questionAnswerDto.getChoicesList().get(0).getIsRight()) {
                    questionAnswerDto.setSolution("A");
                } else if (questionAnswerDto.getChoicesList().get(1).getIsRight()) {
                    questionAnswerDto.setSolution("B");
                }
            } else if (questionAnswerDto.getQuestionType() == 4 || questionAnswerDto.getQuestionType() == 5) {     //处理填空题和简答题
                questionAnswerDto.setSolution(questionAnswerDto.getChoicesList().get(0).getContent()); //一道填空和简答在Answer只有一条记录，直接获取它的content就为正确答案
            }
        }
    }

    //根据题库id和类型id列表查询题目
    @Override
    public List<QuestionAnswerDto> selectQuByType(Integer repositoryId, Integer[] questionType) {
        List<QuestionAnswerDto> list = questionRepositoryDao.selectQuByType(repositoryId, questionType);
        for (QuestionAnswerDto qa : list) {        //循环处理每个选项
            this.handle(qa);
        }

        System.out.println(list.toString());
        return list;
    }

    //删除指定题库的指定题目
    @Override
    public int updateRepositoryQuestion(Integer questionId, Integer repositoryId) {
        return questionRepositoryDao.updateRepositoryQuestion(questionId, repositoryId);
    }

    //根据题目标题查询所有题目
    @Override
    public List<QuestionRepository> selectAllQsBank(String title) {
        return questionRepositoryDao.selectAllQsBank(title);
    }

    @Override
    public int updateQuestion(String content,
                              Integer questionId,
                              Integer questionType,
                              Integer answerCount,
                              String analysis,
                              String solution,//答案
                              List<Answer> list) {
        //更改题目表
        questionRepositoryDao.updateQuestion(content, questionId, answerCount, analysis);

        //更改Answer表
        questionRepositoryDao.deleteALlAnswer(questionId);
        for (Answer answer : list) {
            answer.setQuestionId(questionId);   //设置答案对应的题目id
        }

        if (questionType == 1) {  //处理单选题
            list.get(Integer.parseInt(solution)).setIsRight(true);  //根据前端传过来的solution设置正确选项的isRight=1
        } else if (questionType == 2) {  //处理多选题

            String[] solutions = solution.split(",");  //用逗号把正确答案下标列表分割

            for (String s : solutions) {
                list.get(Integer.parseInt(s)).setIsRight(true);
            }
        } else if (questionType == 3) {   //处理判断

            list.get(Integer.parseInt(solution)).setIsRight(true);//设置正确选项的isRight为1
            list.get(Integer.parseInt(solution)).setContent("对");//设置答案的内容
            if (Integer.parseInt(solution) == 0) {
                list.get(0).setIsRight(true);
                list.get(0).setContent("对");
                list.get(1).setIsRight(false);
                list.get(1).setContent("错");
            }

        } else {   //处理填空和简答
            for (Answer answer : list) {
                answer.setIsRight(true);
                answer.setContent(solution);
            }
        }

        return questionRepositoryDao.insertAnswer(list);
    }

}

